package com.dtagri.fjwlw.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;

import com.dtagri.fjwlw.adapter.viewholders.MonitoringAlarmViewHolder;
import com.dtagri.fjwlw.bean.SmallArea;
import com.dtagri.fjwlw.bean.TjAreaMsg;
import com.dtagri.fjwlw.bean.VRealTimeData;
import com.dtagri.fjwlw.util.StringUtils;

/**
 * @author wxjn
 * 
 */
public class RealTimeDataAdapter extends
		CommonAdapter<TjAreaMsg, MonitoringAlarmViewHolder> {

	public RealTimeDataAdapter(Context context, List<TjAreaMsg> mDataSet) {
		super(context, mDataSet);
	}

	@Override
	public int getCount() {
		int i = 0;
		for (TjAreaMsg tjAreaMsg : mDataSet) {
			List<SmallArea> smallAreas = tjAreaMsg.getSmallAreas();
			for (SmallArea smallArea : smallAreas) {
				List<VRealTimeData> getvRealTimeDatas = smallArea
						.getvRealTimeDatas();
				i += getvRealTimeDatas.size();
			}
		}
		return i;
	}

	@Override
	protected MonitoringAlarmViewHolder createViewHolder() {
		return new MonitoringAlarmViewHolder();
	}

	@Override
	protected void setItemData(int position,
			MonitoringAlarmViewHolder viewHolder, View rootView) {
		// 遍历树得到当前大区\小区\传感器
		int i = 0;
		for (TjAreaMsg tjAreaMsg : mDataSet) {
			List<SmallArea> smallAreas = tjAreaMsg.getSmallAreas();
			for (SmallArea smallArea : smallAreas) {
				List<VRealTimeData> getvRealTimeDatas = smallArea
						.getvRealTimeDatas();
				int j = 0;
				for (VRealTimeData vRealTimeData : getvRealTimeDatas) {
					if (position == i) {
						// curRealTimeData = vRealTimeData;
						viewHolder.tvArea.setText(tjAreaMsg.getAreaName()
								+ " / " + smallArea.getSmallName());
						viewHolder.tvKey.setText(vRealTimeData.getEquipName());
						viewHolder.tvValue.setText(vRealTimeData.getValue()
								+ vRealTimeData.getUnit());
						String reTime = vRealTimeData.getReTime();
						String substring = reTime.substring(0,reTime.lastIndexOf(":"));
//						viewHolder.tvTime.setText(StringUtils.friendly_time(substring));
						viewHolder.tvTime.setText(substring);
						if (j == 0) {
							viewHolder.titleLine.setVisibility(View.VISIBLE);
						} else {
							viewHolder.titleLine.setVisibility(View.GONE);
						}
					}
					i++;
					j++;
				}
			}
		}

	}

}
